<?php

// Include shared script

require_once('shared.php');


$validFrom = '20090520000000';
$validTo = '20090530235959';


$stmt1 = $db->prepare(
	'SELECT			id
	FROM			sherpa_products
	WHERE			articleNumber = ?'
);

$stmt2 = $db->prepare(
	'INSERT INTO	sherpa_prices
					(productId,
					currencyId,
					price,
					oldPrice,
					validFrom,
					validTo,
					sort,
					created)
	VALUES			(?, 1, ?, ?, ?, ?, 1, NOW())'
);


// Process tab separated file

$file = file('../data/aktionspreise.txt');

foreach ($file as $line) {

	if (!preg_match('#^([^\t]+)\t([^\t]+)\t([^\t]+)(\r\n?|\n)?$#', $line, $matches)) {
		continue;
	}

	$articleNumber = $matches[1];
	$price = $matches[2];
	$oldPrice = $matches[3];

	$stmt1->execute(array($articleNumber));

	while ($row = $stmt1->fetch(PDO::FETCH_ASSOC)) {
		$productId = $row['id'];
	
		$stmt2->execute(array(
			$productId,
			$price,
			$oldPrice,
			$validFrom,
			$validTo
		));
	}
}